﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using QuanLyShopCD.GUI;

namespace QuanLyShopCD.BL
{
    class cl_QLCD
    {
        AcessData con;
        Form frm;
        //String SQL;
        DataTable tb;
        public cl_QLCD(frm_QLCD f)
        {
            con = new AcessData();
            frm = f;   
        }
        public void Refresh1()
        {
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
            ((TextBox)frm.Controls["txt_macd1"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_tenloai1"]).DataBindings.Clear();
            ((ComboBox)frm.Controls["comboBox1"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_dongia1"]).DataBindings.Clear();

            ((ComboBox)frm.Controls["comboBox1"]).DataBindings.Add("Text", tb, "MaDM");
            ((TextBox)frm.Controls["txt_tenloai1"]).DataBindings.Add("Text", tb, "TenLoai");
            ((TextBox)frm.Controls["txt_macd1"]).DataBindings.Add("Text", tb, "MaCD");
            ((TextBox)frm.Controls["txt_dongia1"]).DataBindings.Add("Text", tb, "Dongiathue");
        }
        public void ComboBox()
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter("@MaDM", DBNull.Value);
            DataTable tb = con.Execute_ps("ps_LayDanhMuc", pr);
            foreach (DataRow d in tb.Rows)
            {
                ((ComboBox)frm.Controls["comboBox1"]).Items.Add(d["MaDM"]);
            }                      
        }
        public void Hienthi(String MaCD)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            if (MaCD!= null)
                pr[0] = new SqlParameter("@MaCD", MaCD);
            else
                pr[0] = new SqlParameter("@MaCD", DBNull.Value);
            tb = con.Execute_ps("ps_LayCD", pr);

            Refresh1();
        }
        public void Xoa(string MaCD)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter("@MaCD",MaCD);
            tb = con.Execute_ps("ps_XoaCD", pr);
            ((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
            Refresh1();
        }
        public void Sua()
        {
            con.Open();
            string  str_TenLoai,str_MaCD,str_Dongiathue,str_MaDM;
            str_MaCD = ((TextBox)frm.Controls["txt_macd1"]).Text;
            str_MaDM = ((ComboBox)frm.Controls["comboBox1"]).Text;
            str_Dongiathue = ((TextBox)frm.Controls["txt_dongia1"]).Text;
            str_TenLoai = ((TextBox)frm.Controls["txt_tenloai1"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaCD", str_MaCD), new SqlParameter("@TenLoai", str_TenLoai), new SqlParameter("@Dongiathue",str_Dongiathue) };
            tb = con.Execute_ps("ps_SuaCD", pr);
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
            Refresh1();
        }
        public void Them()
        {
            con.Open();
            string str_MaCD, str_TenLoai,str_Dongiathue,str_MaDM;
            str_MaCD = ((TextBox)frm.Controls["txt_macd1"]).Text;
            str_MaDM = ((ComboBox)frm.Controls["comboBox1"]).Text;
            str_TenLoai = ((TextBox)frm.Controls["txt_tenloai1"]).Text;
            str_Dongiathue = ((TextBox)frm.Controls["txt_dongia1"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaCD", str_MaCD), new SqlParameter("@TenLoai", str_TenLoai),new SqlParameter("@Dongiathue",str_Dongiathue),new SqlParameter("@MaDM",str_MaDM) };
            tb = con.Execute_ps("ps_shopcd", pr);

            Refresh1();
        }
    }
}
